#!/usr/bin/perl
use Cwd;
use File::Basename;

my $srcdir;
my $builddir = cwd();
$srcdir = dirname($0) || die "Can't determine \$srcdir.";
chomp $srcdir;

chdir $srcdir;

if (!open( ENTRIES, "$srcdir/entrylist" ) ) {
  print "Can't open $srcdir/entrylist\n";
  exit 1;
}

  while(<ENTRIES>) {
    if (/^#/) { next; }
    chop;
    @entries = split /,/;
    if (!/^.+,(\w+),(\w+)/) { next; }
    push @entryCtrl, @entries[0];
    push @entryRealNames, @entries[1];
    push @entryComments, @entries[2];
    push @entryTypes, @entries[3];
    push @entryNames, @entries[4];
    push @entryCategory, @entries[5];
    push @entryDebug, @entries[6];
  }

close ENTRIES;

if (!open( H_IN, "$srcdir/addressee.src.h" ) ) {
  print "Can't open $srcdir/addressee.src.h\n";
  exit 1;
}
if (!open( H_OUT, ">$builddir/../addressee.h" ) ) {
  print "Can't open addressee.h\n";
  exit 1;
}
  print H_OUT "/*** Warning! This file has been generated by the script makeaddressee ***/\n";

  while( <H_IN> ) {
    if (/--DECLARATIONS--/) {
      for( $i=0; $i<@entryNames; ++$i ) {
        if ( $entryCtrl[$i] =~ /A/ ) {
          print H_OUT "    /**\n";
          print H_OUT "      Set $entryRealNames[$i].\n";
          print H_OUT "     */\n";
          print H_OUT "    void set" . ucfirst($entryNames[$i]);
          print H_OUT "( const $entryTypes[$i] &$entryNames[$i] );\n";

          print H_OUT "    /**\n";
          print H_OUT "      Return $entryRealNames[$i].\n";
          print H_OUT "     */\n";
          print H_OUT "    $entryTypes[$i] $entryNames[$i]() const;\n";
        }

        if ( $entryCtrl[$i] !~ /L/ ) { next; }
        print H_OUT "    /**\n";
        print H_OUT "      Return translated label for $entryNames[$i] field.\n";
        print H_OUT "     */\n";
        print H_OUT "    static QString $entryNames[$i]Label();\n\n";
      }
    } else {
      print H_OUT;
    }
  }

close H_OUT;
close H_IN;

if (!open( CPP_IN, "$srcdir/addressee.src.cpp" ) ) {
  print "Can't open $srcdir/addressee.src.cpp\n";
  exit 1;
}
if (!open( CPP_OUT, ">$builddir/../addressee.cpp" ) ) {
  print "Can't open addressee.cpp\n";
  exit 1;
}
  print CPP_OUT "/*** Warning! This file has been generated by the script makeaddressee ***/\n";

  while( <CPP_IN> ) {
    if (/--VARIABLES--/) {
      for( $i=0; $i<@entryNames; ++$i ) {
        if ( $entryCtrl[$i] !~ /A/ ) { next; }
        print CPP_OUT "    $entryTypes[$i] m".ucfirst($entryNames[$i]).";\n";
      }
    } elsif (/--VARIABLES_ASSIGNMENT--/) {
      for( $i=0; $i<@entryNames; ++$i ) {
        if ( $entryCtrl[$i] !~ /A/ ) { next; }
        print CPP_OUT "      m".ucfirst($entryNames[$i])." = other.m".ucfirst($entryNames[$i]).";\n";
      }
    } elsif (/--DEFINITIONS--/) {
      for( $i=0; $i<@entryNames; ++$i ) {
        if ( $entryCtrl[$i] =~ /A/ ) {
          print CPP_OUT "void Addressee::set" . ucfirst($entryNames[$i]);
          print CPP_OUT "( const $entryTypes[$i] &$entryNames[$i] )\n{\n";
          print CPP_OUT "  if ( $entryNames[$i] == d->m".ucfirst($entryNames[$i])." ) return;\n";
          print CPP_OUT "  d->mEmpty = false;\n";
          print CPP_OUT "  d->m".ucfirst($entryNames[$i])." = $entryNames[$i];\n}\n\n";
          print CPP_OUT "$entryTypes[$i] Addressee::$entryNames[$i]() const\n{\n";
          print CPP_OUT "  return d->m".ucfirst($entryNames[$i]).";\n}\n\n";
        }

        if ( $entryCtrl[$i] !~ /L/ ) { next; }
        @labelwords = split ' ', $entryRealNames[$i];
        for( $j=0; $j < @labelwords; ++$j ) {
          $labelwords[$j] = ucfirst $labelwords[$j];
        }
        $label = join ' ', @labelwords;
        print CPP_OUT "QString Addressee::$entryNames[$i]Label()\n{\n";
        if ( $entryComments[$i] ) {
          print CPP_OUT "  return i18nc(\"$entryComments[$i]\",\"$label\");\n";
        } else {
          print CPP_OUT "  return i18n(\"$label\");\n";
        }
        print CPP_OUT "}\n\n\n";
      }
    } elsif (/--EQUALSTEST--/) {
      for( $i=0; $i<@entryNames; ++$i ) {
        if ( $entryCtrl[$i] =~ /E/ ) {
          if ( $entryNames[$i] !~ "revision" ) {
            if ( $entryTypes[$i] =~ "QString" ) {
              print CPP_OUT "  if ( d->m".ucfirst($entryNames[$i])." != a.d->m".ucfirst($entryNames[$i])." &&\n";
              print CPP_OUT "     !( d->m".ucfirst($entryNames[$i]).".isEmpty() && a.d->m".ucfirst($entryNames[$i]).".isEmpty() ) ) {\n";
              print CPP_OUT "    kDebug(5700) << \"$entryNames[$i] differs\";\n";
              print CPP_OUT "    return false;\n";
              print CPP_OUT "  }\n";
            } else {
              print CPP_OUT "  if ( d->m".ucfirst($entryNames[$i])." != a.d->m".ucfirst($entryNames[$i])." ) {\n";
              print CPP_OUT "    kDebug(5700) << \"$entryNames[$i] differs\";\n";
              print CPP_OUT "    return false;\n";
              print CPP_OUT "  }\n";
            }
          }
        }
      }
    } elsif (/--STREAMOUT--/) {
      for( $i=0; $i<@entryNames; ++$i ) {
        if ( $entryCtrl[$i] =~ /A/ ) {
          print CPP_OUT "  s << a.d->m".ucfirst($entryNames[$i]).";\n";
        }
      }
    } elsif (/--STREAMIN--/) {
      for( $i=0; $i<@entryNames; ++$i ) {
        if ( $entryCtrl[$i] =~ /A/ ) {
          print CPP_OUT "  s >> a.d->m".ucfirst($entryNames[$i]).";\n";
        }
      }
    } elsif (/--DEBUG--/) {
      for( $i=0; $i<@entryNames; ++$i ) {
        if ( $entryCtrl[$i] !~ /A/ ) { next; }
        print CPP_OUT "  str += QString::fromLatin1( \"  " . ucfirst($entryNames[$i]);
        print CPP_OUT ": %1\\n\" ).arg( $entryNames[$i]()$entryDebug[$i] );\n";
      }
    } else {
      print CPP_OUT;
    }
  }

close CPP_OUT;
close CPP_IN;

if (!open( CPP_IN, "$srcdir/field.src.cpp" ) ) {
  print "Can't open $srcdir/field.src.cpp\n";
  exit 1;
}
if (!open( CPP_OUT, ">$builddir/../field.cpp" ) ) {
  print "Can't open field.cpp\n";
  exit 1;
}
  print CPP_OUT "/*** Warning! This file has been generated by the script makeaddressee ***/\n";

  while( <CPP_IN> ) {
    if (/--ENUMS--/) {
      $first = 1;
      for( $i=0; $i<@entryNames; ++$i ) {
        if ( $entryCtrl[$i] !~ /F/ ) { next; }
        if ( $first ) { $first = 0; }
        else { print CPP_OUT ",\n"; }
        print CPP_OUT "      " . ucfirst($entryNames[$i]);
      }
      print CPP_OUT "\n";
    } elsif (/--CASELABEL--/) {
      for( $i=0; $i<@entryNames; ++$i ) {
        if ( $entryCtrl[$i] !~ /F/ ) { next; }
        if ( $entryCtrl[$i] !~ /L/ ) { next; }
        print CPP_OUT "    case Private::" . ucfirst($entryNames[$i]) . ":\n";
        print CPP_OUT "      return Addressee::$entryNames[$i]Label();\n";
      }
    } elsif (/--CASEVALUE--/) {
      for( $i=0; $i<@entryNames; ++$i ) {
        if ( $entryCtrl[$i] !~ /A/ ) { next; }
        if ( $entryCtrl[$i] !~ /F/ ) { next; }
        if ( $entryTypes[$i] ne "QString" ) { next; }
        print CPP_OUT "    case Private::" . ucfirst($entryNames[$i]) . ":\n";
        print CPP_OUT "      return a.$entryNames[$i]();\n";
      }
    } elsif (/--CASESETVALUE--/) {
      for( $i=0; $i<@entryNames; ++$i ) {
        if ( $entryCtrl[$i] !~ /A/ ) { next; }
        if ( $entryCtrl[$i] !~ /F/ ) { next; }
        if ( $entryTypes[$i] ne "QString" ) { next; }
        print CPP_OUT "    case Private::" . ucfirst($entryNames[$i]) . ":\n";
        print CPP_OUT "      a.set" . ucfirst($entryNames[$i]) . "( value );\n";
        print CPP_OUT "      return true;\n";
      }
    } elsif (/--CREATEFIELDS--/) {
      for( $i=0; $i<@entryNames; ++$i ) {
        if ( $entryCtrl[$i] !~ /F/ ) { next; }
        print CPP_OUT "    createField( Private::" . ucfirst($entryNames[$i]);
        if ( $entryCategory[$i] ) {
          print CPP_OUT ", $entryCategory[$i]";
        }
        print CPP_OUT " );\n";
      }
    } else {
      print CPP_OUT;
    }
  }

close CPP_OUT;
close CPP_IN;
